package ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.ToolTipManager;
import javax.swing.text.AbstractDocument;

import data.User;

/**
 * @author Matthias
 * @author NielsChristianMouritsen
 */
public class AdmEdit extends JPanel {
	
	private final JLabel lbl_Firstname;
	private final JLabel lbl_Lastname;
	private final JLabel lbl_userID;
	private final JLabel lbl_PIN;
	private final JLabel lbl_Header;
	private final JTextField textField_Firstname;
	private final JTextField textField_Lastname;
	private final JTextField textField_userID;
	private final JPasswordField passwordField_PIN;
	private final JButton btn_Back;
	private final JButton btn_Submit;
	private final JTextField textField_Streetname;
	private final JTextField textField_Zipcode;
	private final JLabel lbl_Streetname;
	private final JLabel lbl_Streetnr;
	private final JTextField textField_Streetnr;
	private final JLabel lbl_ZipCode;
	private final JTextField textField_City;
	private final JLabel lbl_City;
	private final JLabel lbl_Note;
	private final JTextField textField_Tlf;
	private final JLabel lbl_Tlf;
	private final JComboBox comboBox_Users;
	private final JLabel logo_ATM;
	
	/**Declaring the Screen object **/
	private final Screen screen;
	
	/** Contains the information/blueprint on labels, buttons, combo boxes, password fields and text fields
	 * Sets the window Dimensions and layout
	 * @param screen is implemented as a parameter
	 */
	public AdmEdit(final Screen screen) {
		
		this.screen = screen;
		
		setPreferredSize(new Dimension(600, 480));
		setLayout(null);
		
		lbl_Firstname = new JLabel("Firstname:");
		lbl_Firstname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Firstname.setBounds(278, 131, 82, 21);
		add(lbl_Firstname);
		
		lbl_Lastname = new JLabel("Lastname:");
		lbl_Lastname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Lastname.setBounds(279, 163, 81, 21);
		add(lbl_Lastname);
		
		lbl_userID = new JLabel("ID:");
		lbl_userID.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_userID.setBounds(49, 125, 22, 21);
		add(lbl_userID);
		
		lbl_PIN = new JLabel("PIN:");
		lbl_PIN.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_PIN.setBounds(38, 163, 33, 21);
		add(lbl_PIN);
		
		lbl_Header = new JLabel("Edit User");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(247, 15, 123, 34);
		add(lbl_Header);
		
		textField_Firstname = new JTextField();
		textField_Firstname.addMouseListener(mouseListener);
		textField_Firstname.setToolTipText("characters only");
		((AbstractDocument)textField_Firstname.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_Firstname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Firstname.setBounds(370, 125, 150, 30);
		add(textField_Firstname);
		textField_Firstname.setColumns(10);
		
		textField_Lastname = new JTextField();
		textField_Lastname.addMouseListener(mouseListener);
		textField_Lastname.setToolTipText("letters and whitespaces");
		((AbstractDocument)textField_Lastname.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_Lastname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Lastname.setBounds(370, 159, 150, 30);
		add(textField_Lastname);
		textField_Lastname.setColumns(10);
		
		textField_userID = new JTextField();
		textField_userID.setEditable(false);
		textField_userID.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_userID.setBounds(81, 123, 150, 30);
		add(textField_userID);
		textField_userID.setColumns(10);
		
		passwordField_PIN = new JPasswordField();
		passwordField_PIN.addMouseListener(mouseListener);
		passwordField_PIN.setToolTipText("4 digits only");
		((AbstractDocument)passwordField_PIN.getDocument()).setDocumentFilter(new LimitOfChar(4));
		passwordField_PIN.setFont(new Font("SansSerif", Font.PLAIN, 16));
		passwordField_PIN.setBounds(80, 159, 150, 30);
		add(passwordField_PIN);
		
		textField_Streetname = new JTextField();
		textField_Streetname.addMouseListener(mouseListener);
		textField_Streetname.setToolTipText("letters and whitespaces allowed");
		((AbstractDocument)textField_Streetname.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_Streetname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetname.setBounds(370, 224, 150, 30);
		add(textField_Streetname);
		textField_Streetname.setColumns(10);
		
		textField_Zipcode = new JTextField();
		textField_Zipcode.addMouseListener(mouseListener);
		textField_Zipcode.setToolTipText("4-5 digits required");
		((AbstractDocument)textField_Zipcode.getDocument()).setDocumentFilter(new LimitOfChar(5));
		textField_Zipcode.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Zipcode.setBounds(370, 298, 150, 30);
		add(textField_Zipcode);
		textField_Zipcode.setColumns(10);
		
		textField_Streetnr = new JTextField();
		textField_Streetnr.addMouseListener(mouseListener);
		textField_Streetnr.setToolTipText("letters, digits, whitespaces and commas");
		((AbstractDocument)textField_Streetnr.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_Streetnr.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetnr.setBounds(370, 262, 150, 30);
		add(textField_Streetnr);
		textField_Streetnr.setColumns(10);
		
		textField_City = new JTextField();
		textField_City.addMouseListener(mouseListener);
		textField_City.setToolTipText("letters and whitespaces allowed");
		((AbstractDocument)textField_City.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_City.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_City.setBounds(370, 334, 150, 30);
		add(textField_City);
		textField_City.setColumns(10);
				
		textField_Tlf = new JTextField();
		textField_Tlf.addMouseListener(mouseListener);
		textField_Tlf.setToolTipText("8-15 digits required");
		((AbstractDocument)textField_Tlf.getDocument()).setDocumentFilter(new LimitOfChar(15));
		textField_Tlf.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Tlf.setBounds(370, 191, 150, 30);
		add(textField_Tlf);
		textField_Tlf.setColumns(10);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(38, 390, 130, 50);
		add(btn_Back);
		
		btn_Submit = new JButton("Submit");
		btn_Submit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Submit.setBounds(390, 390, 130, 50);
		add(btn_Submit);

		lbl_Streetname = new JLabel("Streetname:");
		lbl_Streetname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetname.setBounds(267, 228, 93, 21);
		add(lbl_Streetname);
		
		lbl_Streetnr = new JLabel("Street nr.:");
		lbl_Streetnr.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetnr.setBounds(285, 266, 75, 21);
		add(lbl_Streetnr);
		
		lbl_ZipCode = new JLabel("Zip code:");
		lbl_ZipCode.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_ZipCode.setBounds(289, 302, 71, 21);
		add(lbl_ZipCode);
		
		lbl_City = new JLabel("City:");
		lbl_City.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_City.setBounds(324, 338, 36, 21);
		add(lbl_City);
		
		lbl_Note = new JLabel("<html><p>Note: ID is not <br> editable as well <br> as Admin</p></html>");
		lbl_Note.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		lbl_Note.setBounds(38, 210, 150, 74);
		add(lbl_Note);
		
		lbl_Tlf = new JLabel("Telephone:");
		lbl_Tlf.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Tlf.setBounds(273, 195, 87, 21);
		add(lbl_Tlf);
		
		comboBox_Users = new JComboBox();
		comboBox_Users.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		comboBox_Users.setBounds(81, 66, 205, 45);
		addAllUsers();
		add(comboBox_Users);

		logo_ATM = new JLabel("");
		logo_ATM.setIcon(new ImageIcon(AdmEdit.class.getResource("/pic/LOGO Mini.png")));
		logo_ATM.setBounds(15, 15, 50, 50);
		add(logo_ATM);
	}
		
	/** Adds action listeners
	 * @param event is a parameter for the action listeners
	 */
	public void addActionListener(final ActionListener event) {
		btn_Submit.addActionListener(event);
		btn_Back.addActionListener(event);
	}
	
	/** Adds item listener for the combo box
	 * @param item is a variable
	 */
	public void addItemListener(final ItemListener item){
		comboBox_Users.addItemListener(item);
	}
	
	/** Gets the information from the user from the arraylist in BankDatabase
	 * @param userID is the variable that is used to get the specific data from the arraylist
	 */
	public void showUserDataFields(final int userID) {
		for (User usr : screen.getClassATM().getClassBankDatabase().getUserList()) {
			if(usr.getUserID() == userID) {
				textField_userID.setText(""+usr.getUserID());
				passwordField_PIN.setText(""+usr.getPIN());
				textField_Firstname.setText(""+usr.getFirstName());
				textField_Lastname.setText(""+usr.getLastName());
				textField_Tlf.setText(""+usr.getTelephoneNr());
				textField_Streetname.setText(""+usr.getStreetName());
				textField_Streetnr.setText(""+usr.getStreetNr());
				textField_Zipcode.setText(""+usr.getZipCode());
				textField_City.setText(""+usr.getCity());
			}
		}
	}
	
	/**
	 * Sets the admin account to non-editable, since its hardcoded and is not in the DB
	 */
	public void setAdminNotEditable() {
		passwordField_PIN.setEditable(false);
		textField_Firstname.setEditable(false);
		textField_Lastname.setEditable(false);
		textField_Tlf.setEditable(false);
		textField_Streetname.setEditable(false);
		textField_Streetnr.setEditable(false);
		textField_Zipcode.setEditable(false);
		textField_City.setEditable(false);
	}
	
	/**
	 * Sets the text fields to being editable for all users except the admin
	 */
	public void setEditable() {
		textField_Firstname.setEditable(true);
		textField_Lastname.setEditable(true);
		textField_Tlf.setEditable(true);
		textField_Streetname.setEditable(true);
		textField_Streetnr.setEditable(true);
		textField_Zipcode.setEditable(true);
		textField_City.setEditable(true);
	}
	
	/**
	 * @return submit button
	 */
	public JButton getBtnSubmit() {
		return btn_Submit;
	}
	
	/**
	 * @return back button 
	 */
	public JButton getBtnBack() {
		return btn_Back;
	}
	
	/**
	 * @return combo box 
	 */
	public JComboBox getComboBox() {
		return comboBox_Users;
	}

	/**
	 * @return entered first name text field
	 */
	public String getTextField_Firstname() {
		return textField_Firstname.getText().trim();
	}

	/**
	 * @return entered last name text field
	 */
	public String getTextField_Lastname() {
		return textField_Lastname.getText().trim();
	}

	/**
	 * @return entered user ID text field
	 */
	public String getTextField_userID() {
		return textField_userID.getText().trim();
	}

	/**
	 * @return entered pin password field
	 */
	public int getPasswordField_PIN() {
		char[] pin = passwordField_PIN.getPassword();
		return Integer.parseInt(new String(pin));
	}

	/**
	 * @return entered street name text field
	 */
	public String getTextField_Streetname() {
		return textField_Streetname.getText().trim();
	}

	/**
	 * @return entered zip-code text field
	 */
	public String getTextField_Zipcode() {
		return textField_Zipcode.getText().trim();
	}

	/**
	 * @return entered street number text field
	 */
	public String getTextField_Streetnr() {
		return textField_Streetnr.getText().trim();
	}
	
	/**
	 * @return entered city text field
	 */
	public String getTextField_City() {
		return textField_City.getText();
	}
	
	/**
	 * @return entered telephone text field
	 */
	public String getTextField_Tlf(){
		return textField_Tlf.getText().trim();
	}
	
	/**
	 * Clears data at startup
	 * Adds items to item list for the user selected
	 */
	public void addAllUsers(){
		comboBox_Users.removeAllItems();
		for (User usr : screen.getClassATM().getClassBankDatabase().getUserList()) {
			comboBox_Users.addItem("User ID: " + usr.getUserID());
		}
		screen.pack();
	}
	
	/** 
	 * @return the selected userId of the combobox
	 */
	public int getSpecificUserID(){
		int[] info = new int[screen.getClassATM().getClassBankDatabase().getUserList().size()];
	    for (int i = 0; i < screen.getClassATM().getClassBankDatabase().getUserList().size(); i++) {
			info[i] = screen.getClassATM().getClassBankDatabase().getUserList().get(i).getUserID();
		}
	    return info[comboBox_Users.getSelectedIndex()];
	}
	
	/** Adding a mouselistener to the class, by instantiating a MouseAdapter in an anonymous class 
	 * When the mouse is hovering over an object, the tooltip is enabled
	 * When the mouse is not hovering over an object, the tooltop is disabled
	 */
	MouseListener mouseListener = new MouseAdapter(){    
	    final int defaultTimeout = ToolTipManager.sharedInstance().getInitialDelay();
	    @Override
	    public void mouseEntered(final MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(0);
	    }

	    @Override
	    public void mouseExited(final MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(defaultTimeout);
	    }
	};
	
} // end class AdmEdit